﻿using System;
using System.Collections;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.DAL;
using KPIS.GERP.GIMS.MODEL;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Receive_Object : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        private D_IcmMasResourceItem d_resource_item = new D_IcmMasResourceItem();
        private MasterDepreciationTypeDAL d_masdep_type = new MasterDepreciationTypeDAL();
        //private M_IcmMasResourceItem m_resource_item = new M_IcmMasResourceItem();
        private D_IcmDeliveryNote dnDAL = new D_IcmDeliveryNote();
        private D_IcmMasObjectType ObTypeDAL = new D_IcmMasObjectType();
        //private ICM_ReceiveBLL ReceiveBLL = new ICM_ReceiveBLL();
        private D_Asset d_asset = new D_Asset();
        private D_IcmMasResourceItem ResItemDAL = new D_IcmMasResourceItem();
        private D_IcmReceiveObject d_receive = new D_IcmReceiveObject();
        private M_IcmReceiveObject m_receive = new M_IcmReceiveObject();
        
        // By KHIM
        private ResourceMasterTypeBLL masBLL = new ResourceMasterTypeBLL();
        private MasterWarehouseBLL whBLL = new MasterWarehouseBLL();
        private DeliveryNoteBLL dnBLL = new DeliveryNoteBLL();
        private HrmJobSiteBLL jsBLL = new HrmJobSiteBLL();
        
        //**********
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblReceiveType.Text = Request.QueryString["pt"];
                CheckType(lblReceiveType.Text);

                rdoWH.Checked = true;
                rdoWH.Attributes.Add("onclick", "return selectWH('" + cmbWareHouse.ClientID + "', '" + cmbToJobSite.ClientID + "')");
                rdoJS.Attributes.Add("onclick", "return selectJS('" + cmbWareHouse.ClientID + "', '" + cmbToJobSite.ClientID + "')");
            }
        }
        protected void CheckType(string Type)
        {
            bool bTranfer = Type == "1";
            bool bDN = Type == "2";

            //Panel1.Visible = bDN;
            PanelTransfer.Visible = bTranfer;

        }
    #region Tab รับวัสดุเข้าคลัง
        protected void btnEdit_Click(object sender, EventArgs e)
        {
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (PanelReceive.Visible)
            {
            //m_receive.trans_mas_seq = 1;// ไม่รู้คืออะไร
                m_receive.icm_receive_code = fnGen_ReceiveCode();
                m_receive.icm_dn_seq = Convert.ToInt32(cmbDN.SelectedValue);
                m_receive.icm_wh_seq = cn.ConvertNullInt(cmbWareHouse.SelectedValue);
                m_receive.job_site_seq = cn.ConvertNullInt(cmbToJobSite.SelectedValue);
                m_receive.icm_res_type_seq = Convert.ToInt32(cmbInvType.SelectedValue);
                m_receive.receive_type_seq = cn.ConvertNullInt(lblReceiveType.Text.Trim());
                m_receive.receive_date = cn.ConvertNullDateTime(txtReceiveDate.TextWithLiterals);
                m_receive.tranfer_ref_no = txtTranRefNo.Text.Trim();
                m_receive.created_by = Convert.ToInt32(Session["EmpSeq"].ToString());
                d_receive.Insert_Receive(m_receive);
            }
            else if (PanelReceiveItem.Visible)
            {
                m_receive.quantity = Convert.ToDouble(txtQuantity.Text);
                m_receive.quantity_remain = Convert.ToDouble(txtQuantity.Text);
                m_receive.quantity_remain_after = Convert.ToDouble(txtQuantity.Text);
                m_receive.quantity_remain_before = Convert.ToDouble(txtQuantity.Text);
                m_receive.price = Convert.ToDouble(txtUnitPrice.Text);
                //d_receive.in
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
        }
        protected void cmbDN_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            // By KHIM *********************************************
            IList<DeliveryNoteInfo> infoList = dnBLL.GetDeliveryNote((int?)null, (int?)null, (int?)null, (int?)null, (int?)null, SystemInfo._ApproveRecordStatus);

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbDN.Items.Add(new RadComboBoxItem(infoList[i].RefNo, infoList[i].Seq.ToString()));

                TableRow tr = new TableRow();

                TableCell tdRefNo = new TableCell();
                Label lblRefNo = new Label();
                lblRefNo.Text = infoList[i].RefNo;
                tdRefNo.Width = Unit.Pixel(100);
                tdRefNo.Controls.Add(lblRefNo);
                tr.Cells.Add(tdRefNo);

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(250);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);

                TableCell tdDnDate = new TableCell();
                Label lblDnDate = new Label();
                lblDnDate.Text = infoList[i].DeliveryDate.ToString();
                tdDnDate.Width = Unit.Pixel(100);
                tdDnDate.Controls.Add(lblDnDate);
                tr.Cells.Add(tdDnDate);

                ((Table)cmbDN.Items[j].FindControl("tableDN")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        //protected void cmbDn_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    cmbChooseInventoryItem.Text = "";            
        //    txtPrice.Text = "";// table.Rows[i]["item_price_include_vat"].ToString();
        //    txtQuantity.Text = "";// table.Rows[i]["item_qty"].ToString();
        //}
        //protected void LoadDetailItem(string DnSeq, string ItemSeq)
        //{
        //    DataSet ListDataCommand = dnDAL.GetItemByDn(DnSeq, ItemSeq);
        //    DataTable table = ListDataCommand.Tables[0];
        //    txtPrice.Text = table.Rows[0]["item_price_include_vat"].ToString();
        //    txtQuantity.Text = table.Rows[0]["item_qty"].ToString();
        //    txtReDate.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["receive_date"]);

        //}
        //protected void cmbChooseInventoryItem_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        //{
        //    DataSet ListDataCommand = dnDAL.GetItemByDn(cmbDn.SelectedValue,"");
        //    DataTable table = ListDataCommand.Tables[0];          

        //    //IList<M_IcmMasResourceItem> infoList = d_resource_item.GetListFillByName(e.Text);
        //    int itemOffset = e.NumberOfItems;
        //    int endOffset = Math.Min(itemOffset + 10, table.Rows.Count);
        //    //int endOffset = Math.Min(itemOffset + 10, infoList.Count);
        //    //e.EndOfItems = (endOffset == infoList.Count);
        //    int j = 0;
        //    for (int i = itemOffset; i < endOffset; i++)
        //    {
        //        cmbChooseInventoryItem.Items.Add(new RadComboBoxItem(table.Rows[i]["icm_res_item_name"].ToString(), table.Rows[i]["icm_res_item_seq"].ToString()));
        //        //cmbChooseInventoryItem.Items.Add(new RadComboBoxItem(infoList[i].icm_res_item_name, infoList[i].icm_res_item_seq.ToString()));
        //        TableRow tr = new TableRow();
        //        TableCell tdCode = new TableCell();
        //        Label lblCode = new Label();
        //        lblCode.Text = table.Rows[i]["icm_res_item_code"].ToString();
        //        //lblCode.Text = infoList[i].icm_res_item_code;
        //        tdCode.Width = Unit.Pixel(100);
        //        tdCode.Controls.Add(lblCode);
        //        tr.Cells.Add(tdCode);
        //        TableCell tdName = new TableCell();
        //        Label lblName = new Label();
        //        lblName.Text = table.Rows[i]["icm_res_item_name"].ToString();
        //        //lblName.Text = infoList[i].icm_res_item_name;
        //        tdName.Width = Unit.Pixel(190);
        //        tdName.Controls.Add(lblName);
        //        tr.Cells.Add(tdName);
        //        TableCell tdDescr = new TableCell();
        //        Label lblDescr = new Label();
        //        lblDescr.Text = table.Rows[i]["icm_res_item_descr"].ToString();
        //        //lblDescr.Text = infoList[i].icm_res_item_descr;
        //        tdDescr.Width = Unit.Pixel(450);
        //        tdDescr.Controls.Add(lblDescr);
        //        tr.Cells.Add(tdDescr);
        //        ((Table)cmbChooseInventoryItem.Items[j].FindControl("tableInventoryItem")).Controls.Add(tr);
        //        j++;
        //    }

        //    e.Message = GetStatusMessage(endOffset, table.Rows.Count);
        //    //e.Message = GetStatusMessage(endOffset, infoList.Count);
        //}
        //protected void cmbChooseInventoryItem_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    LoadDetailItem(cmbDn.SelectedValue, cmbChooseInventoryItem.SelectedValue);

        //    M_IcmMasResourceItem infoRes = ResItemDAL.GetListFillBySeq(cn.ConvertNullInt(cmbChooseInventoryItem.SelectedValue));
        //    fncGen_AssetCode(infoRes.icm_res_item_code);
        //}
        protected void cmbWareHouse_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            // By KHIM *********************************************
            IList<MasterWarehouseInfo> infoList = whBLL.GetMasterWarehouse();

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbWareHouse.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));

                TableRow tr = new TableRow();

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(250);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                ((Table)cmbWareHouse.Items[j].FindControl("tableWareHouse")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbToJobSite_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            // By KHIM *********************************************
            IList<HrmJobSiteInfo> infoList = jsBLL.GetJobSite(e.Text);

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbToJobSite.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));

                TableRow tr = new TableRow();

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(250);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                ((Table)cmbToJobSite.Items[j].FindControl("tableToJobSite")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbInvType_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            // By KHIM *********************************************
            IList<ResourceMasterTypeInfo> infoList = masBLL.GetResourceMasterType();

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbInvType.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                
                TableRow tr = new TableRow();

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(250);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);                
                ((Table)cmbInvType.Items[j].FindControl("tableInventoryType")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
            //*************************END**********************************
            //IList<M_IcmMasObjectType> infoList = ObTypeDAL.GetList();
            //int itemOffset = e.NumberOfItems;
            //int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            //e.EndOfItems = (endOffset == infoList.Count);
            //int j = 0;
            //for (int i = itemOffset; i < endOffset; i++)
            //{
            //    cmbInvType.Items.Add(new RadComboBoxItem(infoList[i].icm_obj_type_name.ToString(), infoList[i].icm_obj_type_seq.ToString()));

            //    TableRow tr = new TableRow();
            //    TableCell tdCode = new TableCell();
            //    Label lblCode = new Label();
            //    lblCode.Text = infoList[i].icm_obj_type_code.ToString();
            //    tdCode.Width = Unit.Pixel(1000);
            //    tdCode.Controls.Add(lblCode);
            //    tr.Cells.Add(tdCode);

            //    TableCell tdName = new TableCell();
            //    Label lblName = new Label();
            //    lblName.Text = infoList[i].icm_obj_type_name;
            //    tdName.Width = Unit.Pixel(250);
            //    tdName.Controls.Add(lblName);
            //    tr.Cells.Add(tdName);
            //    ((Table)cmbInvType.Items[j].FindControl("tableInventoryType")).Controls.Add(tr);
            //    j++;
            //}
            

        }
        protected void cmbDpType_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<MasterDepreciationTypeInfo> infoList = d_masdep_type.GetMasterDepreciationType();
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbDpType.Items.Add(new RadComboBoxItem(infoList[i].Name.ToString(), infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].Seq.ToString();
                tdCode.Width = Unit.Pixel(30);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(380);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                ((Table)cmbDpType.Items[j].FindControl("tableDpType")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);

        }
        private string fnGen_ReceiveCode()
        {
            string strMonth = "";
            string strYear = "";
            string strRunning = "";
            string strReceiveCode = "";
            try
            {
                //DataSet ListDataCommand = ReceiveBLL.GenReceiveCode();
                //DataTable table = ListDataCommand.Tables[0];
                //strYear = table.Rows[0]["YearValue"].ToString();
                //strMonth = cn.ConvertstrMonth(table.Rows[0]["MonthValue"].ToString());
                //strRunning = cn.ConvertstrRunningFiveDigit(table.Rows[0]["running"].ToString());
                //strReceiveCode = "RC" + strYear + "-" + strMonth + "-" + strRunning;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return strReceiveCode;
        }
        private string fncGen_AssetCode(string ResItemCode)
        {
            string strMonth = "";
            string strYear = "";
            string strRunning = "";
            string strAsset = "";
            try
            {
                //DataSet ListDataCommand = ReceiveBLL.GenReceiveCode();
                //DataTable table = ListDataCommand.Tables[0];
                //strYear = table.Rows[0]["YearValue"].ToString();
                //strMonth = cn.ConvertstrMonth(table.Rows[0]["MonthValue"].ToString());
                //strRunning = cn.ConvertstrRunningFiveDigit(table.Rows[0]["running"].ToString());
                //strAsset = strYear + strMonth + "-" + ResourceItem + "-" + strRunning;
                M_Asset infoAsset = d_asset.GenerateAssetCode(ResItemCode);
                strYear = infoAsset.stryear;
                strMonth = cn.ConvertstrMonth(infoAsset.strmonth);
                strRunning = cn.ConvertstrRunningThreeDigit(infoAsset.assetcode_running);
                strAsset = strYear + strMonth + "-" + ResItemCode + "-" + strRunning;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return strAsset;
        }

    #endregion

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        //protected void rbDepreAbove_CheckedChanged(object sender, EventArgs e)
        //{
        //}
        //protected void rbDepreBelow_CheckedChanged(object sender, EventArgs e)
        //{
        //}       
    }
}
